package device_map

import (
	"context"
	"encoding/base64"
	"strings"

	"device-admin/internal/app/grpc/protoc/devicec"
	"device-admin/internal/app/grpc/service/pubsub"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"device-admin/internal/models"

	"192.168.1.75/go-pkg/helper"
	"google.golang.org/protobuf/proto"
)

// Resume98Volume 恢复98的音量
func ResumeRobotVolume(ctx context.Context, robotID string, records []*models.TaskRecord) {
	volumeSettingReq := &devicec.VolumeSettingReq{}

	// 获取98音量配置
	volume := dao.Terminal{}.GetVolumeSetting(context.Background(), dal.Q, robotID, "98", 2)
	// 获取当前机器的工作类型，执行任务时，恢复声音
	for _, record := range records {
		if !strings.Contains(strings.ToLower(record.TaskName), "charging") || record.WorkType != 2 {
			if volume == -1 {
				volumeSettingReq.Percent = 55
			} else {
				volumeSettingReq.Percent = int32(volume)
			}
		}
	}
	volumeSettingReq.Volumetype = 1
	// 根据任务类型，自动关闭98声音
	if volumeSettingReq.Percent > 0 {
		sendMsg := &devicec.Msg{}
		sendMsg.Ack = 0
		sendMsg.Seq = helper.RandString(16)
		sendMsg.Name = string(volumeSettingReq.ProtoReflect().Descriptor().FullName())
		sendMsg.Content, _ = proto.Marshal(volumeSettingReq)
		// protobuf序列化
		bytes, _ := proto.Marshal(sendMsg)
		pubsub.CMDPublish(robotID, "98", "msg", base64.StdEncoding.EncodeToString(bytes))
	}
}
